﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace BLL.LibrarySys
{
    /// <summary>
    /// T_PUB_LIBRARYMATERIAL
    /// </summary>
    public partial class T_PUB_LIBRARYMATERIAL
    {
        private readonly DALL.LibrarySys.T_PUB_LIBRARYMATERIAL dal = new DALL.LibrarySys.T_PUB_LIBRARYMATERIAL();
        public T_PUB_LIBRARYMATERIAL()
        { }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetMaterialList(int bid, int lid, string titel, string typeid)
        {
            string strWhere = "";
            if (bid != -1)
            {
                strWhere += string.Format(" and b.BRANCH_ID = {0}", bid);
            }
            if (lid != -1)
            {
                strWhere += string.Format(" and c.LIBRARY_ID = {0}", lid);
            }
            if (!string.IsNullOrEmpty(titel))
            {
                strWhere += string.Format(" and MATERIAL_NAME like '%{0}%'", titel);
            }
            if (!string.IsNullOrEmpty(typeid))
            {
                strWhere += string.Format(" and TYPE_ID in ({0})", typeid);
            }
            return dal.GetMaterialList(strWhere);
        }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsMaterial(int? LID, int? MID)
        {
            return dal.ExistsMaterial(LID, MID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.LibrarySys.T_PUB_LIBRARYMATERIAL model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.LibrarySys.T_PUB_LIBRARYMATERIAL model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 入库更新数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="outmodel"></param>
        public void Update_Material(Model.LibrarySys.T_PUB_LIBRARYMATERIAL model, Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL outmodel)
        {
            dal.Update_Material(model, outmodel);
        }

        /// <summary>
        /// 入库更新数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="outmodel"></param>
        public void Add_Material(Model.LibrarySys.T_PUB_LIBRARYMATERIAL model, Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL outmodel)
        {
            dal.Add_Material(model, outmodel);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void UpdateLibraryMaterial(Model.LibrarySys.T_PUB_LIBRARYOUT model, IList<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> updateList)
        {
             dal.UpdateLibraryMaterial(model, updateList);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void UpdateLibrary(Model.LibrarySys.T_PUB_LIBRARYMATERIAL model, Model.LibrarySys.T_PUB_LIBRARYOUT libraryout)
        {
             dal.UpdateLibrary(model, libraryout);
        }

        //public void AddPurchase(Model.Procurement.T_PUB_WAREHOUSING model, Model.LibrarySys.T_PUB_LIBRARYMATERIAL lmmodel)
        //{
        //    dal.AddPurchase(model, lmmodel);
        //}

        public void AddPurchase(Model.Procurement.T_PUB_WAREHOUSING model, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> materiallist, IList<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> addList, IList<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> editList)
        {
            dal.AddPurchase(model, materiallist, addList, editList);
        }

        //public void EditPurchase(Model.Procurement.T_PUB_WAREHOUSING model, Model.LibrarySys.T_PUB_LIBRARYMATERIAL lmmodel)
        //{
        //    dal.EditPurchase(model, lmmodel);
        //}

        /// <summary>
        /// 更新一条数据
        /// </summary>
        //public void UpdateMaterial(Model.LibrarySys.T_PUB_LIBRARYOUT model, Model.LibrarySys.T_PUB_LIBRARYMATERIAL LIBRARYMATERIALModeel)
        //{
        //     dal.UpdateMaterial(model, LIBRARYMATERIALModeel);
        //}

        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool UpdateMaterial(int Lid, int Mid, int Num)
        //{
        //    return dal.UpdateMaterial(Lid, Mid, Num);
        //}

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            return dal.Delete(ID);
        }
        
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.LibrarySys.T_PUB_LIBRARYMATERIAL GetModel(int ID)
        {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.LibrarySys.T_PUB_LIBRARYMATERIAL GetModel(int bid, int mid)
        {

            return dal.GetModel(bid, mid);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
 
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> GetListArrayByBranch(int branch_id)
        {
            string strWhere = string.Format(" LIBRARY_ID = {0}", branch_id);
            return dal.GetListArray(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> DataTableToList(DataTable dt)
        {
            List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> modelList = new List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.LibrarySys.T_PUB_LIBRARYMATERIAL model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
                    if (dt.Rows[n]["ID"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    if (dt.Rows[n]["LIBRARY_ID"] != null && dt.Rows[n]["LIBRARY_ID"].ToString() != "")
                    {
                        model.LIBRARY_ID = int.Parse(dt.Rows[n]["LIBRARY_ID"].ToString());
                    }
                    if (dt.Rows[n]["MATERIAL_ID"] != null && dt.Rows[n]["MATERIAL_ID"].ToString() != "")
                    {
                        model.MATERIAL_ID = int.Parse(dt.Rows[n]["MATERIAL_ID"].ToString());
                    }
                    if (dt.Rows[n]["NUM"] != null && dt.Rows[n]["NUM"].ToString() != "")
                    {
                        model.NUM = decimal.Parse(dt.Rows[n]["NUM"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        #endregion  Method
    }
}
